package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    /**
     * @Author: 15041
     * @Date: 18:26 2021/1/18
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 登录成功返回信息
    */
    @RequestMapping("loginSuccess")
    public Result loginSuccess() {
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 18:27 2021/1/18
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 登录失败返回信息
    */
    @RequestMapping("loginFail")
    public Result loginFail() {
        return new Result(false, MessageConst.LOGIN_FAIL);
    }

    /**
     * @Author: 15041
     * @Date: 18:27 2021/1/18
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     *Description: 获取当前登录用户 用户名回显
    */
    @GetMapping("getUsername")
    public Result getUsername() {
        //获取当前登录用户
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof User) {
            User user = (User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }

}
